/*******************************************************************************
 * Copyright (c) 2005 Bastian Bergerhoff and others
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution.
 * 
 * Contributors:
 *     Bastian Bergerhoff - initial API and implementation
 *******************************************************************************/
package org.outlook.eclipse.plugins.regexp4eclipse.dialogs;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.outlook.eclipse.plugins.regexp4eclipse.Messages;

/**
 * @author bastian.bergerhoff
 */
public class SimpleTextDialog extends Dialog {

	private Text textField;

	private String title;

	private String contents;

	/**
	 * The constructor
	 * 
	 * @param p_parentShell
	 * @param p_title
	 * @param p_contents
	 */
	public SimpleTextDialog(Shell p_parentShell, String p_title,
			String p_contents) {
		super(p_parentShell);
		this.title = p_title;
		this.contents = p_contents;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets
	 * .Composite)
	 */
	protected Control createDialogArea(Composite parent) {
		getShell().setText(title);
		// create a composite with standard margins and spacing
		Composite composite = new Composite(parent, SWT.NONE | SWT.RESIZE);
		GridLayout layout = new GridLayout();
		layout.marginHeight = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN);
		layout.marginWidth = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN);
		layout.verticalSpacing = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING);
		layout.horizontalSpacing = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING);
		layout.numColumns = 1;
		composite.setLayout(layout);
		composite.setLayoutData(new GridData(GridData.FILL_BOTH));
		applyDialogFont(composite);
		textField = new Text(composite, SWT.READ_ONLY | SWT.LEAD | SWT.MULTI
				| SWT.H_SCROLL | SWT.V_SCROLL | SWT.RESIZE);
		GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_FILL
				| GridData.VERTICAL_ALIGN_FILL);
		gd.grabExcessHorizontalSpace = true;
		gd.grabExcessVerticalSpace = true;
		gd.heightHint = 250;
		gd.widthHint = 400;
		textField.setLayoutData(gd);
		textField.setBackground(new Color(getShell().getDisplay(), new RGB(255,
				255, 255)));
		textField.setText(contents);

		return composite;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.jface.dialogs.Dialog#createButtonsForButtonBar(org.eclipse
	 * .swt.widgets.Composite)
	 */
	protected void createButtonsForButtonBar(Composite parent) {
		createButton(parent, IDialogConstants.CANCEL_ID, Messages
				.getString("dialogs.SimpleTextDialog.button.close"), true);
		Button exportButton = createButton(parent, IDialogConstants.PROCEED_ID,
				Messages.getString("dialogs.SimpleTextDialog.button.export"),
				true);
		exportButton.addListener(SWT.Selection, new Listener() {

			@Override
			public void handleEvent(Event event) {
				System.out.println("click1");
				FileDialog fileDialog = new FileDialog(getShell(), SWT.SAVE);
				boolean flag = false;
				File file = null;
				while (!flag) {
					String fileName = fileDialog.open();
					System.out.println("fileName: " + fileName);
					file = new File(fileName);
					if (file.exists()) {
						MessageBox mb = new MessageBox(fileDialog.getParent(), SWT.ICON_WARNING | SWT.YES | SWT.NO);
						mb.setMessage(fileName + Messages.getString("dialogs.SimpleTextDialog.alert.file.exist"));
						flag = mb.open() == SWT.YES;
					}
					flag = true;
				}
				Writer writer = null;
				try {
					writer = new FileWriter(file);
					writer.write(contents);
					writer.flush();
				} catch (IOException e) {
					e.printStackTrace();
				} finally {
					try {
						writer.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		});
	}
}